package com.ziyun.erp.modules.promotion.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.service.common.protocol.BaseResult;
import com.service.timer.api.TimerService;
import com.utility.JsonUtils;
import com.ziyun.erp.modules.goods.entity.GoodsProductEntity;
import com.ziyun.erp.modules.promotion.dao.PromotionPolicyConfigDao;
import com.ziyun.erp.modules.promotion.entity.PromotionPolicyConfigEntity;
import com.ziyun.erp.modules.promotion.service.PromotionPolicyConfigService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("promotionPolicyConfigService")
public class PromotionPolicyConfigServiceImpl extends ServiceImpl<PromotionPolicyConfigDao, PromotionPolicyConfigEntity> implements PromotionPolicyConfigService {
    @Autowired
    private PromotionPolicyConfigDao promotionPolicyConfigDao;
    @Autowired
    private TimerService timerService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<PromotionPolicyConfigEntity> page = this.selectPage(
                new Query<PromotionPolicyConfigEntity>(params).getPage(),
                new EntityWrapper<PromotionPolicyConfigEntity>()
                        .eq(StringUtils.isNotBlank((String) params.get("policyType")), "policy_type", (String) params.get("policyType"))
                        .like(StringUtils.isNotBlank((String) params.get("name")), "`name`", (String) params.get("name"))
                        .eq("disabled", 0)
        );

        return new PageUtils(page);
    }

    @Override
    public R deletebyIds(Integer[] ids) {
        int i = promotionPolicyConfigDao.deletebyIds(ids);
        if (i <= 0) {
            return R.error("删除失败");
        }
        return R.ok();
    }

    @Override
    public R updateEntityById(PromotionPolicyConfigEntity promotionPolicyConfig) {
        //全角半角空串
        String debarScope = promotionPolicyConfig.getDebarScope().replaceAll("，",",").replaceAll("，",",").replaceAll(" ","");
        promotionPolicyConfig.setDebarScope(debarScope);
        Integer count = this.baseMapper.updateAllColumnById(promotionPolicyConfig);
        if (count < 1) {
            return R.error("修改失败,请联系管理员!");
        }
        return R.ok();
    }

    @Override
    public R insertEntity(PromotionPolicyConfigEntity promotionPolicyConfig) {
        //赠品只能是标品
        if ("fullgive".equals(promotionPolicyConfig.getResultType())) {
            List<Map<String,Object>>  resultParamList = JsonUtils.string2Collection( promotionPolicyConfig.getResultParam(),List.class,Map.class);
            Boolean flag=true;
            for (Map<String, Object> resultParam : resultParamList) {
                GoodsProductEntity goodsProductEntity = this.baseMapper.getAllGoodsType(Long.valueOf(resultParam.get("sku").toString()));
                if (goodsProductEntity == null) {
                    flag=false;
                    break;
                }
            }
            if (!flag) {
                return R.error("满赠只能赠送标品!");
            }
        }

        String debarScope = promotionPolicyConfig.getDebarScope().replaceAll("，",",").replaceAll("，",",").replaceAll(" ","");
        promotionPolicyConfig.setDebarScope(debarScope);
        this.baseMapper.insert(promotionPolicyConfig);
        //调用接口
        BaseResult<?> baseResult = timerService.promotionById(promotionPolicyConfig.getId());
        return R.ok();
    }

//    @Override
//    public List<Map<String, Object>> getAllGoodsType() {
//        return this.baseMapper.getAllGoodsType();
//    }

}
